MEGpipeline_AfniResampleAnat.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Checks dims & orientation of AFNI anatomy file. %
  3. % Resamples anatomy to dimensions & orientation of functional image. %
  4. % Last modified: Feb. 25, 2014 %
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. %
  7. % Usage:
  8. % OutputBrik = MEGpipeline_AfniResampleAnat(AnatBrik, FuncBrik)
  9. %
  10. % Inputs:
  11. % AnatBrik = AFNI anatomy file to check (and resample to functional if needed).
  12. % FuncBrik = AFNI functional file that anatomy will be compared to.
  13. %
  14. % Output:
  15. % OutputBrik = If resample was needed, path of resampled AFNI anatomy is output.
  16. % If resample was not needed, path of original AnatBrik is output.
  17. % Copyright (C) 2013-2014, Michael J. Cheung
  18. %
  19. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  20. % details, see the documentation included with the software package.
  21. %
  22. % MEGPLS is free software: you can redistribute it and/or modify it under
  23. % the terms of the GNU General Public License version 2 as published by
  24. % the Free Software Foundation. This program is distributed in the hope
  25. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  26. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. % See the GNU General Public License for more details.
  28. %
  29. % You should have received a copy of the GNU General Public License along
  30. % with this program. If not, you can download the license here:
  31. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  32. function OutputBrik = MEGpipeline_AfniResampleAnat(AnatBrik, FuncBrik)
  33. if ~exist(AnatBrik, 'file')
  34. disp('Error: Specified AFNI anatomy file does not exist.');
  35. OutputBrik = [];
  36. return;
  37. end
  38. if ~exist(FuncBrik, 'file')
  39. disp('Error: Specified AFNI functional file does not exist.');
  40. OutputBrik = [];
  41. return;
  42. end
  43. % Assemble paths and remove existing files:
  44. [FuncFolder, FuncFile, ~] = fileparts(FuncBrik);
  45. [AnatFolder, AnatFile, ~] = fileparts(AnatBrik);
  46. AfniViewLabel = FuncFile(end-4:end);
  47. AnatFile(end-4:end) = []; % Remove +view to get just filename
  48. ResampledName = [AnatFolder,'/',AnatFile,'_resampled'];
  49. ResampledBrik = [AnatFolder,'/',AnatFile,'_resampled',AfniViewLabel,'.BRIK'];
  50. if exist(ResampledBrik, 'file')
  51. delete([ResampledName,AfniViewLabel,'.BRIK']);
  52. delete([ResampledName,AfniViewLabel,'.HEAD']);
  53. end
  54. % Load BRIK files:
  55. CurrentDir = pwd;
  56. Opt.format = 'vector';
  57. cd(AnatFolder); % Work-around for occasional bug
  58. [~, Anat, AnatInfo, ~] = BrikLoad(AnatBrik, Opt);
  59. cd(FuncFolder);
  60. [~, Func, FuncInfo, ~] = BrikLoad(FuncBrik, Opt);
  61. cd(CurrentDir);
  62. % Compare AnatBrik to FuncBrik to see if resample needed:
  63. AnatNumDims = length(size(Anat));
  64. FuncNumDims = length(size(Func));
  65. if AnatNumDims < FuncNumDims
  66. CheckFuncDim = size(Func);
  67. CheckFuncDim = CheckFuncDim(1:AnatNumDims);
  68. elseif AnatNumDims > FuncNumDims
  69. CheckFuncDim = size(Anat);
  70. CheckFuncDim = CheckFuncDim(1:FuncNumDims);
  71. else
  72. CheckFuncDim = size(Func);
  73. end
  74. AnatOrient = AnatInfo.ORIENT_SPECIFIC;
  75. FuncOrient = FuncInfo.ORIENT_SPECIFIC;
  76. if ~isequal(size(Anat), CheckFuncDim) || ~isequal(AnatOrient, FuncOrient)
  77. system(['3dresample -master ',FuncBrik,' -inset ',AnatBrik,' -prefix ',ResampledName]);
  78. OutputBrik = ResampledBrik;
  79. else
  80. OutputBrik = AnatBrik;
  81. end